//电磁辐射 算法特征值
package com.safety.algorithm.eigenvalue.electromagneticRadiation;

import com.safety.algorithm.model.electromagneticRadiation.ElectromagneticRadiationAlgModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ElectromagneticRadiationAlgorithm {

//    //电磁辐射趋势预警指标,传入v1,v2需要的数据,m*2时间，
//    public List<ElectromagneticRadiationWarningModel> warnings(List<ElectromagneticRadiationAlgModel> listsv1, List<ElectromagneticRadiationAlgModel> listsv2) {
//        List<ElectromagneticRadiationWarningModel> result = new ArrayList<ElectromagneticRadiationWarningModel>();
//        int lenv1 = listsv1.size();
//        int lenv2 = listsv2.size();
//        double v1 = 0;
//        double v2 = 0;
//        double dt = 15 * 24 * 60;//δt 为可设置参数，取值范围30min-15day，此处用15天
//        double A;//电磁辐射趋势预警指标
//        for (int i = 0; i < lenv1; i++) {
//            v1 += listsv1.get(i).getMonitoringValue();
//        }
//        for (int i = 0; i < lenv2; i++) {
//            v2 += listsv2.get(i).getMonitoringValue();
//        }
//        A = Math.abs(v1 - v2) / dt;
//        ElectromagneticRadiationWarningModel electromagneticRadiationWarningModel =
//                new ElectromagneticRadiationWarningModel(listsv2.get(lenv2 - 1).getDatetime(), listsv2.get(lenv2 - 1).getEntId(), A);
//        result.add(electromagneticRadiationWarningModel);
//        return result;
//    }

    //电磁辐射趋势预警指标
    public double warnings(double v1, double v2, double dt) {
        return Math.abs(v1 - v2) / dt;
    }

    //计算m×3个数据的强度平均值
    public double average(List<ElectromagneticRadiationAlgModel> lists) {
        double sum = 0;
        double count = lists.size();
        for (ElectromagneticRadiationAlgModel list : lists) {
            if (list.getMonitoringValue() != -1) {
                sum += list.getMonitoringValue();
            } else {
                count--;
            }
        }
        return sum / count;
    }

    //n小时偏差值 传入n小时的数据，每m分钟计算一次，m默认为3分钟，传入m*3分钟的值以求出平均值
    public double dev(double enNow, double enAverage) {
        return enNow / enAverage - 1;
    }

    //偏离异常持续时间h，阈值为a，a默认值0.5,返回1为+m，返回0为归零
    public static List<Map<String, Object>> devAbnormal(List<Map<String, Object>> lists, int count, int m) {
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        double devHT = 0;
        int num = 0;//记录连续加的有多长
        for (Map<String, Object> map : lists) {
            if (map.get("flag").toString().equals("1")) {
                num++;
                if (num <= count) {
                    devHT += m;
                }
            } else {
                devHT = 0;
                num = 0;
            }
            map.put("value", devHT);
            result.add(map);
        }
        return result;
    }

    //n小时偏离异常累计时间H 传入n小时的数据
    public static List<Map<String, Object>> devAbnormalTotal(List<Map<String, Object>> lists, int count, int m) {
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        double devHT = 0;
        int num = 0;
        for (int i = 0; i < lists.size(); i++) {
            num++;
            if (lists.get(i - count).get("flag").toString().equals("1") && num > count) {
                devHT -= m;
            }
            if (lists.get(i).get("flag").toString().equals("1")) {
                devHT += m;
            }
            lists.get(i).put("value", devHT);
            result.add(lists.get(i));
        }
        return result;
    }


    //n小时偏离异常频次N 传入n小时数据
    public static List<Map<String, Object>> devAbnormalFrequency(List<Map<String, Object>> lists, int count) {
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        double devHT = 0;
        int num = 0;
        for (int i = 0; i < lists.size(); i++) {
            num++;
            if (lists.get(i - count).get("flag").toString().equals("1") && num > count) {
                devHT--;
            }
            if (lists.get(i).get("flag").toString().equals("1")) {
                devHT++;
            }
            lists.get(i).put("value", devHT);
            result.add(lists.get(i));
        }
        return result;
    }


}
